#!/bin/bash

######################################################################
#
#  Copyright (c) 2015 arakasi72 (https://github.com/arakasi72)
#
#  --> Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
#
######################################################################

sourcedir='http://rtorrent.net/downloads/'
rt_latest=$(wget -q -O - $sourcedir | grep -o "rtorrent-[0-9]\{1,2\}\.[0-9]\{1,2\}\.[0-9]\{1,2\}" | sort -uV | tail -1 | grep -o "[0-9]\{1,2\}\.[0-9]\{1,2\}\.[0-9]\{1,2\}")
ru_latest=$(curl -s https://raw.githubusercontent.com/Novik/ruTorrent/master/js/webui.js | grep -m 1 version: | cut -d \" -f2)

# checks if an application is installed
installed() {
hash $1 2>/dev/null
}

if ( installed rtorrent ); then
  rt_current=$(rtorrent -h | grep -om 1 "[0-9]\{1,2\}\.[0-9]\{1,2\}\.[0-9]\{1,2\}")
  echo "rtorrent installed: $rt_current"
  echo "rtorrent available: $rt_latest"
else
  echo "rtorrent: not installed"
fi

echo

if [ -d /var/www/rutorrent ]; then
  ru_current=$(grep -m 1 version: /var/www/rutorrent/js/webui.js | cut -d \" -f2)
  echo "RuTorrent installed: $ru_current"
  echo "RuTorrent available: $ru_latest"
else
  echo "RuTorrent: not installed"
fi

echo

echo "rtinst"
cd /etc/rtinst
branch=$(basename "$(git symbolic-ref -q HEAD)")
if [ -z $branch ]; then
  branch=$(git describe --tags --exact-match)
  echo "Current Release: rtinst $branch"
  if [ ${branch:0:1} = v ]; then
    latest=$(basename "$(git ls-remote --tags https://github.com/arakasi72/rtinst.git | grep -o 'refs/tags/v.*' | sort -V | tail -1)")
    if [ $branch = $latest ]; then
      echo "This is the latest release"
    else
      echo "Use rtsetup to update to rtinst $latest"
    fi   
  fi
else
  echo "Current Branch: $branch"
  echo "Use rtsetup to update branch"
fi
